package com.zle.action;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zle.dao.IpersonDao;
import com.zle.dao.bean.Person;

@RestController()
@RequestMapping("/modal1")
public class DemoActon {
	@Autowired
	private IpersonDao personDao;
	 
	@GetMapping(value="/persion")
	public List<Person> say(){
		return personDao.findAll();
	}
	@GetMapping(value="/persion/add")
	public Person add(@RequestParam("name") String name,@RequestParam("age") String age){
		Person person = new Person();
		person.setName(name);
		person.setAge(age);
		return personDao.save(person);
	}
	@GetMapping(value="/persion/name")
	public List<Person> findByname(@RequestParam("name") String name){
		return personDao.findByName(name);
	}
	
	@GetMapping(value="/persion/add2")
	public Person add2(@Valid Person person,BindingResult result){
		if(result.hasErrors()){
			System.out.println(result.getObjectName());
			System.out.println(result.getFieldError());
			return null;
		}
		return personDao.save(person);
	}

}
